using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class UserControls_PhotoUploaderUc : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public byte[] FileBytes
    {
        get 
        {
            //if (!cbxSavePhoto.Checked)
            //{
            //    return null;
            //}
            Stream inputStream = null;
            try
            {
                HttpPostedFile postedFile = photoUpload.PostedFile;
                inputStream = postedFile.InputStream;
                byte[] byteBuffer = new byte[inputStream.Length];
                inputStream.Read(byteBuffer, 0, byteBuffer.Length);
                return byteBuffer;
            }
            catch
            {
                return null;
            }
            finally
            {
//                IMS_LIB.util.StreamUtil.safeClose(inputStream);
            }
        }
    }

    public void Clear()
    {
        ImageUrl = "";
    }

    //public String FileName
    //{
    //    get 
    //    {
    //        if (!cbxSavePhoto.Checked)
    //        {
    //            return "";
    //        }
    //        try
    //        {
    //            return photoUpload.PostedFile.FileName;
    //        }
    //        catch
    //        {
    //            return "";
    //        }
    //    }
    //}

    public Boolean ImageVisible
    {
        get { return serverPhotoPreview.Visible; }
        set { serverPhotoPreview.Visible = value; }
    }

    public Boolean SavePhoto
    {
        get { return cbxSavePhoto.Checked; }
    }

    public Boolean Enabled
    {
        get { return cbxSavePhoto.Enabled; }
        set 
        { 
            cbxSavePhoto.Enabled = value;
            photoUpload.Disabled = !(value && cbxSavePhoto.Checked);
        }
    }

    public String ImageUrl
    {
        get { return serverPhotoPreview.ImageUrl; }
        set 
        {
            if (value.Equals(""))
            {
                serverPhotoPreview.ImageUrl = "../image_loader.aspx?passengerId=0";
                //ImageVisible = false;
            }
            else
            {
                serverPhotoPreview.ImageUrl = value;
                //ImageVisible = true;
            }
        }
    }
    protected void cbxSavePhoto_CheckedChanged(object sender, EventArgs e)
    {
        photoUpload.Disabled = !cbxSavePhoto.Checked;
    }
}
